package com.yisu.expressway.manager.core.service.impl;

import com.yisu.expressway.ewf.common.web.EasyUIGrid;
import com.yisu.expressway.ewf.entity.OrderMain;
import com.yisu.expressway.ewf.entity.OrderSub;
import com.yisu.expressway.ewf.entity.RefundRecord;
import com.yisu.expressway.manager.core.mapper.OrderMainMapper;
import com.yisu.expressway.manager.core.mapper.OrderSubMapper;
import com.yisu.expressway.manager.core.mapper.RefundRecordMapper;
import com.yisu.expressway.manager.core.mapper.SpecificationGoodsMapper;
import com.yisu.expressway.manager.core.service.IOrderService;
import com.yisu.expressway.manager.domain.request.GoodsInfoRequest;
import com.yisu.expressway.manager.domain.request.OrderRequest;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * /**
 * User: 赵杰
 * Date: 2017/5/8
 * Time: 17:01
 * Desc: 订单模块相关业务处理.
 */
@Service
public class OrderServiceImpl implements IOrderService {
    @Resource
    private OrderMainMapper orderMainMapper;
    @Resource
    private OrderSubMapper orderSubMapper;
    @Resource
    private SpecificationGoodsMapper specificationGoodsMapper;
    @Resource
    private RefundRecordMapper refundRecordMapper;


    /**
     * 分页获取订单列表信息
     *
     * @param orderRequest
     * @return
     */
    @Override
    public EasyUIGrid<OrderMain> getOrderMainListByPage(OrderRequest orderRequest) {
        List<OrderMain> orderMainList = orderMainMapper.getOrderMainListByPage(orderRequest);
        EasyUIGrid<OrderMain> gridData = new EasyUIGrid<>();
        gridData.setTotal(orderRequest.getTotalRecords());
        gridData.setRows(orderMainList);
        return gridData;
    }

    /**
     * 通过订单编号获取订单主信息
     *
     * @param orderId
     * @return
     */
    @Override
    public OrderMain getOrderMainByOrderId(long orderId) {
        return orderMainMapper.selectByPrimaryKey(orderId);
    }

    /**
     * 通过订单编号获取订单子表信息
     *
     * @param orderId
     * @return
     */
    @Override
    public List<OrderSub> getOrderSubByOrderId(long orderId) {
        return orderSubMapper.getOrderSubListByOrderId(orderId);
    }

    /**
     * 编辑订单主表信息
     *
     * @param orderMain
     * @return
     */
    @Override
    public boolean modifyOrderMain(OrderMain orderMain) {
        return orderMainMapper.updateByPrimaryKeySelective(orderMain) > 0;
    }

    /**
     * 取消订单
     *
     * @param orderMain 订单
     * @return 成功，失败
     */
    @Override
    public boolean cancelOrder(OrderMain orderMain) {
        try {
            OrderMain orderMainVo = orderMainMapper.selectByPrimaryKey(orderMain.getOrderId());
            if (orderMainVo == null)
                return false;
            OrderMain updateModel = new OrderMain();
            updateModel.setOrderId(orderMainVo.getOrderId());
            //如果订单已经支付，则进入退款流程；反之则直接取消订单
            if (orderMainVo.getOrderStatus() >= (OrderMain.OrderStatus.WAIT_DELIVER_GOODS.code)) {
                updateModel.setOrderStatus(OrderMain.OrderStatus.CANCEL_WAIT_PAY.code);
            } else {
                updateModel.setOrderStatus(OrderMain.OrderStatus.CANCEL_ORDER.code);
            }
            updateModel.setCancelTime(new java.sql.Timestamp(new java.util.Date().getTime()));
            if (orderMainMapper.updateByPrimaryKeySelective(updateModel) > 0) {
                if (updateModel.getOrderStatus().equals(OrderMain.OrderStatus.CANCEL_WAIT_PAY.code)) {
                    //追加退款流水
                    RefundRecord refundRecord=new RefundRecord();
                    refundRecord.setStatus(RefundRecord.StatusEnum.WAIT_REFUND.code);
                    refundRecord.setOrderId(orderMainVo.getOrderId());
                    refundRecord.setRefundAmount(orderMainVo.getOrderAmount());
                    refundRecord.setReason(orderMain.getOrderAbstract());
                    refundRecord.setCreateBy("sys");
                    refundRecordMapper.insert(refundRecord);
                }
                List<OrderSub> orderSubList = orderSubMapper.getOrderSubListByOrderId(orderMain.getOrderId());
                if (!CollectionUtils.isEmpty(orderSubList)) {
                    for (OrderSub orderSub : orderSubList) {
                        //订单取消，补充库存
                        GoodsInfoRequest goodsInfoRequest = new GoodsInfoRequest();
                        goodsInfoRequest.setGoodsNo(orderSub.getGoodsNo());
                        goodsInfoRequest.setSpecificationNo(orderSub.getSpecificationNo());
                        goodsInfoRequest.setCategoryCode(String.valueOf(orderSub.getCategoryCode()));
                        goodsInfoRequest.setSubtractInventoryNum(orderSub.getBuyNumber());
                        specificationGoodsMapper.addInventoryNum(goodsInfoRequest);
                    }
                }
            }
            return true;
        } catch (Exception ex) {
            return false;
        }
    }
}
